package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.compilation.GovernmentMoneyDto;
import com.dg.cuckoo.admin.dto.other.FileDto;
import com.dg.cuckoo.admin.repository.FileRepository;
import com.dg.cuckoo.admin.repository.GoveMoneyRepository;
import com.dg.cuckoo.admin.service.GovernmentMoneyService;
import com.dg.cuckoo.common.utils.Oss;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class GovernmentMoneyServiceimpl implements GovernmentMoneyService {

    @Autowired
    private GoveMoneyRepository repository;
    @Autowired
    private FileRepository fileRepository;

    @Transactional
    @Override
    public void save(GovernmentMoneyDto dto, MultipartFile[] files) throws Exception {
        powerfulSave(dto,files);
    }

    @Transactional
    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择要删除的行");
        }
        repository.deleteById(id);
        delects(id);
    }

    @Override
    public PageModel page(Integer page, Integer pageSize) {
        return powerfulPage(page,pageSize,1);
    }

    @Override
    public GovernmentMoneyDto openInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择要打开的行");
        }
        return repository.findById(id).get();
    }

    @Override
    public String openContent(String id) {
        if(id==null){
            throw new ServiceException("请选择要打开的行");
        }
        String contentBox = repository.findById(id).get().getContentBox();
        return contentBox;
    }

    @Transactional
    @Override
    public void update(String id,GovernmentMoneyDto dto, MultipartFile[] files) throws Exception {
        powerfulUpdate(id,dto,files);
    }

    @Override
    public void powerfulSave(GovernmentMoneyDto dto, MultipartFile[] files) throws Exception {
        checkSave(dto);
        GovernmentMoneyDto save = repository.save(dto);
        if (files!=null){
            filesSave(files,save.getId());
        }
    }

    @Override
    public void powerfulUpdate(String id, GovernmentMoneyDto dto, MultipartFile[] files) throws Exception {
        if(id==null){
            throw new ServiceException("请选择要打开的行");
        }
        checkSave(dto);
        dto.setId(id);
        repository.save(dto);
        if (files!=null){
            delects(id);
            filesSave(files,id);
        }
    }

    @Override
    public PageModel powerfulPage(Integer page, Integer pageSize, Integer type) {
        Integer one = page;
        page = (page - 1) * pageSize;
        int size = repository.findByType(type).size();
        List<GovernmentMoneyDto> list = repository.page(type,page, pageSize);
        return new PageModel(one, pageSize, size, list);
    }


    private void delects(String id){
        List<FileDto> byMark = fileRepository.findByByMark(id);
        fileRepository.deleteAll(byMark);
    }

    private void checkSave(GovernmentMoneyDto dto){

    }

    public  void filesSave(MultipartFile[] files,String id) throws Exception {
        for (MultipartFile file:files){
            String url = Oss.load(file);
            FileDto fileDto = new FileDto();
            fileDto.setFileUrl(url);
            fileDto.setByMark(id);
            fileRepository.save(fileDto);
        }
    }
}
